import React, { useEffect, useState } from 'react';
import { history } from 'umi';
import { message } from 'antd';

import { connect } from 'dva';

const getQueryValue = (queryName) => {
  const url = window.location.href;
  let p = url.split('?')[1];
  let params = new URLSearchParams(p);
  const ticket = params.get(queryName);
  return ticket;
};

const loginAuth = (props) => {
  // const menuData = JSON.parse(sessionStorage.getItem('menuData'));
  // const menu = menuData[0] && menuData[0]['children'];
  // if (menu) {
  //   const paht = menu[0] && menu[0]['path'] ? menu[0]['path'] : null;
  //   paht && history.push(`${paht}`);
  // }

  const { dispatch } = props;

  useEffect(() => {
    const uId = sessionStorage.getItem('onlyKey');
    const auth = sessionStorage.getItem('author');
    if (auth) {
      const resData = JSON.parse(sessionStorage.getItem('menuData'));
      const chilPath =
        resData[0]['children'] && resData[0]['children'].length > 0
          ? resData[0]['children'][0]['path']
          : resData[0]['path'];
      history.push(chilPath);
    } else if (uId && !auth) {
      dispatch({
        type: 'base_login/getTokenByUId',
        payload: { uId },
      }).then(({ code, msg, resData }) => {
        if (code && code === 200) {
          if (resData.length > 0) {
            const chilPath =
              resData[0]['children'] && resData[0]['children'].length > 0
                ? resData[0]['children'][0]['path']
                : resData[0]['path'];
            history.push(chilPath);
          }
        } else {
          message.error(msg);
        }
      });
    } else {
      window.location.href = 'http://27.196.3.67:22002/isc_sso/login?service=';
    }
  }, []);

  /** 获取url地址 ticket参数 */

  return <div></div>;
};

export default connect()(loginAuth);
